package com.yt.user.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.yt.user.models.dto.SysUserDTO;
import com.yt.user.models.service.ISysUserService;
import com.yt.user.models.vo.LoginInfoVO;
import com.yt.user.models.vo.SysUserVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.yt.user.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yt.user.mapper.LoginInfoMapper;
import com.yt.user.models.service.ILoginInfoService;
import com.yt.user.models.bo.LoginInfoSaveBO;
import com.yt.user.models.dto.LoginInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

/**
 * 登录信息控制器
 *
 * @author lizicong
 * @Date 2025-09-05 15:14:07
 */
@DubboService
public class LoginInfoServiceImpl extends ServiceImpl<LoginInfoMapper, LoginInfoDTO> implements ILoginInfoService{

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    @Lazy
    private ISysUserService ISysUserService;

    @Override
    public boolean add(LoginInfoSaveBO saveBO) {
        LoginInfoDTO dto = new LoginInfoDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public LoginInfoVO getLogin(LoginInfoSaveBO loginInfoSaveBO) {
        Wrapper  wrapper = new EntityWrapper();
        wrapper.eq("account",loginInfoSaveBO.getAccount())
                .eq("password",loginInfoSaveBO.getPassword());
        LoginInfoDTO loginInfoDTO = this.selectOne(wrapper);
        if(null != loginInfoDTO){
            LoginInfoVO loginInfoVO = new LoginInfoVO();
            BeanUtils.copyProperties(loginInfoDTO,loginInfoVO);
            return loginInfoVO;
        }

        return null;
    }

    @Override
    public SysUserVO getUserlogin(LoginInfoSaveBO loginInfoSaveBO) {
        LoginInfoVO loginInfoVO =getLogin(loginInfoSaveBO);
        if(null == loginInfoVO){
            return null;
        }

        SysUserVO sysUserVO = new SysUserVO();
        //获取用户信息
        SysUserDTO sysUserDTO = ISysUserService.selectById(loginInfoVO.getUserId());
//        if(loginInfoVO.getPassword().equals(Md5Utils.MD5Encode(loginInfoSaveBO.getPassword(),loginInfoVO.getSalt()))){
//            BeanUtils.copyProperties(sysUserDTO,sysUserVO);
//            return sysUserVO;
//        }
        if (loginInfoVO.getPassword().equals(loginInfoSaveBO.getPassword())){
            BeanUtils.copyProperties(sysUserDTO, sysUserVO);
            return sysUserVO;
        }
        return null;
    }
}
